package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.analyse.tool.SelectType;
import com.kdtech.analyse.tool.SubstrLogic;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class PeopleNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		if(url.startsWith("http://tv.")){
			return false;
		}
		String[] regex={
				"http://.*[.]people[.]com[.]cn/.*/[0-9]{4}/[0-9]{4}/[a-z]{1}[0-9]+-[0-9]+[\\-0-9]*.html",
				"http://.*[.]people[.]com[.]cn/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]+[\\-0-9]*.html",
				"http://.*[.]people.com.cn/GB/[0-9]+/[0-9]+[\\-0-9]*.html",
				"http://.*[.]people.com.cn/m/phone/news.cqr[0-9]+\\?Num=[0-9]+&windowsapp=.*",

				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";;
		String author="";
		String url=urlMeta.getUrl();
		Long date=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		Document doc=Jsoup.parse(html);


		doc.select(".list_xg").remove();
		doc.select(".fr").remove();
		doc.select("div.sc_zwdb").remove();
		doc.select("#zoom p  a").remove();

		doc.select("div.hot_day").remove();
		doc.select("div.about").remove();
		doc.select("div.c_r").remove();
		doc.select("[style=text-indent: 2em] a,[style=text-indent: 2em] strong:containsOwn(推荐阅读)").remove();
		doc.select("strong:containsOwn(最新动态：)").remove();
		doc.select("div.jingbian2012,div.hot_day").remove();
		doc.select("div.text_show p a").remove();

		/*
		 * 解析新闻标题
		 */
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("h1#p_title"));
		tool.addParseLogic(new ParseLogic("td#p_title"));
		tool.addParseLogic(new ParseLogic("h1"));
		tool.addParseLogic(new ParseLogic("td.title_news"));
		tool.addParseLogic(new ParseLogic("div.news-title"));
		title=tool.parse();
		Pattern p=Pattern.compile(".*【([0-9]+)?】");
        Matcher m=p.matcher(title);
        if (m.find()) {
        	String group=m.group(1);
        	title=title.replace("【"+group+"】","");
        }

		/*
		 * 解析时间
		 */
		tool.addParseLogic(new ParseLogic("#p_publishtime,div.fr"));
		tool.addParseLogic(new ParseLogic("td.ly_red"));
		tool.addParseLogic(new ParseLogic("div.info"));
		tool.addParseLogic(new ParseLogic("h2"));
		tool.addParseLogic(new ParseLogic("h3"));
		tool.addParseLogic(new ParseLogic("h5"));
		tool.addParseLogic(new ParseLogic("td.STYLE59"));
		tool.addParseLogic(new ParseLogic("div.news-area1"));
		date=tool.parseDate();
		if (date==null){
			date=JSoupUtils.matchDate(doc, "来源：");
		}


		/*
		 * 解析内容
		 */
		doc.select("div.zdfy,div.box,div[align=center] a").remove();
		doc.select("div#p_content u").remove();
		tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("#p_content"));
		tool.addParseLogic(new ParseLogic("td.fbody"));
		tool.addParseLogic(new ParseLogic("div.text_show"));
		tool.addParseLogic(new ParseLogic("p.pictext"));
		tool.addParseLogic(new ParseLogic("#zoom"));
		tool.addParseLogic(new ParseLogic("div.news-body"));
		content=tool.parse();

		if (content!=null && content.contains("热点新闻")){
			content=StringUtils.substringBefore(content, "热点新闻");
		}

		/*
		 * 解析作者
		 */
		 author= JSoupUtils.matchAuthor(doc, "来源：");

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);

		return meta;


	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		String url="http://fanfu.people.com.cn/n/2014/0812/c64371-25447606.html";
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		PeopleNewsAnalyse cnhubeiNewsAnalyse=new PeopleNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if(detailPage){
		NewsMeta parserHtml =cnhubeiNewsAnalyse.parserHtml(urlMeta);
		System.out.println(parserHtml);
		cnhubeiNewsAnalyse.Update(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}



	
	public boolean isNeedUpdate(){
		return false;
	}
}
